/**
 * Created by Administrator on 2016/8/29.
 */
var btn = document.getElementsByTagName("input");
preBtn = btn[0];
inBtn = btn[1];
postBtn = btn[2];
treeRoot = document.getElementsByClassName('root')[0];
divList=[];
timer = null;
window.onload = function(){
    preBtn.onclick = function (){
        reset();
        preOrder(treeRoot);
        changeColor();
}
    inBtn.onclick = function (){
        reset();
        inOrder(treeRoot);
        changeColor();
    }

    postBtn.onclick = function (){
        reset();
        postOrder(treeRoot);
        changeColor();
    }

}
//二叉树前序遍历
function preOrder(node){
    if(node != null){
        divList.push(node);
        preOrder(node.firstElementChild);
        preOrder(node.lastElementChild);
    }
}
//二叉树中序遍历
function inOrder(node){
    if(node != null){
        inOrder(node.firstElementChild);
        divList.push(node);
        inOrder(node.lastElementChild);
    }
}

//二叉树后序遍历
function postOrder(node){
    if(node != null){
        postOrder(node.firstElementChild);
        postOrder(node.lastElementChild);
        divList.push(node);
    }
}

function changeColor(){
    var i = 0;
    divList[i].style.backgroundColor = 'blue';
    timer = setInterval(function(){
        i++;
//        alert(divList[i-1])
        if(i<divList.length){
            divList[i-1].style.backgroundColor = '#fff';
            divList[i].style.backgroundColor = 'blue';
        }else{
            clearInterval(timer);
            divList[divList.length-1].style.backgroundColor = '#fff';
        }
    },500)
}

function reset(){
    divList = [];
    clearInterval(timer);
    var divs = document.getElementsByTagName('div');
    for(var i = 0;i<divs.length;i++){
        divs[i].style.backgroundColor = '#fff';
    }
}
